package com.example.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.navigation.R;

public class HomeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //获取Button控件
        Button button = getView().findViewById(R.id.button);
        button.setOnClickListener(view -> {
            //通过bundle传递数据
            /*Bundle args = new Bundle();
            args.putString("userName" , "jack");*/

            //通过safe args传递数据
            //通过插件自动创建的Args来获取bundle对象 并且设置数据
            Bundle args = new HomeFragmentArgs.Builder()
                    .setUserName("Jack")
                    .setAge(18)
                    .build()
                    .toBundle();
            //将控件传入navController 会自动在主视图中找到
            NavController navController = Navigation.findNavController(view);
            //通过navigate方法跳转页面
            navController.navigate(R.id.action_homeFragment_to_detailFragment, args);
        });
    }
}